(function (util, win) {
    var locationObj = {
        lat: '',
        lng: '',
        areaName: ''
    };
    win.h5Location = {};
    //var geolocation = new BMap.Geolocation();
    //var myGeo = new BMap.Geocoder();
    var geolocation ='';
    var myGeo ='';
     var init = function(cb) {
     //util.loadScript("https://api.map.baidu.com/api?v=2.0&ak=p6ZFMsyLpVntdPCDvYtaNQ7z6qHG24L6", function () {
     //    geolocation = new BMap.Geolocation();
     //    myGeo = new BMap.Geocoder();
     //    cb && cb();
         //});
         geolocation = new BMap.Geolocation();
         myGeo = new BMap.Geocoder();
         cb && cb();
     };
    //浏览器定位
    h5Location.browserLocation = function (succ, fail) {
        let self = this;
        init(function () {

            geolocation.getCurrentPosition(function (res) {
                if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                    locationObj.lat = res.point.lat;
                    locationObj.lng = res.point.lng;
                    myGeo.getLocation(new BMap.Point(res.point.lng, res.point.lat), function (res) {
                        if (res) {
                            locationObj.areaName = res.addressComponents.district;
                            succ && succ(locationObj);
                        }
                    });
                }
                else {
                    fail && fail();
                }
            });


        })
       
    }

    //ip定位  
    h5Location.getIpLocation = function (succ, fail) {
        init(function () {

            var myCity = new BMap.LocalCity().get(function (res) {
                console.log(res);
                if (res) {
                    locationObj.lat = res.center.lat;
                    locationObj.lng = res.center.lng;
                    myGeo.getLocation(new BMap.Point(res.center.lng, res.center.lat), function (res) {
                        if (res) {
                            console.log(res);
                            locationObj.areaName = res.addressComponents.district;
                            succ && succ(locationObj);
                        }
                    });
                } else {
                    fail && fail();
                }
            });


        })
      


    }

})(util, window);